package com.snailhd.entity;

import com.alibaba.fastjson.JSON;
import com.snailhd.constant.MACD;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.math.BigDecimal;

/**
 * @author hd
 * @date 2019-10-09 00:58
 */

@Setter
@Getter
@ToString
@NoArgsConstructor
public class StockDay {

    private String code;

    private Integer date;
    /** 成交额 */
    private BigDecimal amount;
    /** 相对于昨收的价格变动百分比，单位% */
    private BigDecimal changepercent;
    /** 最高 */
    private BigDecimal high;
    /** 最低 */
    private BigDecimal low;
    /** 今开 */
    private BigDecimal open;
    /** 相对于昨收的价格变动 */
    private BigDecimal pricechange;
    /** 昨收 */
    private BigDecimal settlement;
    /** 当前价格 */
    private BigDecimal trade;
    /** 成交量 */
    private BigDecimal volume;

    private BigDecimal ema12;
    private BigDecimal ema26;
    private BigDecimal dea;
    private BigDecimal macd;

    public BigDecimal emaDif(){
        return this.ema12.subtract(this.ema26);
    }


    public BigDecimal calcMacd(StockDay preDay){


        BigDecimal preEma12 = preDay.getEma12();
        BigDecimal preEma26 = preDay.getEma26();

        if(MACD.b0.compareTo(preEma12)==0){ preEma12 = preDay.getTrade(); }
        if(MACD.b0.compareTo(preEma26)==0){ preEma26 = preDay.getTrade(); }

        /*
         * ema12 = (前一日ema12 * 11 + trade * 2)/13
         */
        this.ema12 = preEma12.multiply(MACD.b11).add(trade.multiply(MACD.b2)).divide(MACD.b13,4);

        /*
         *  ema26 = (前一日ema26 * 25 + trade * 2)/27
         */
        this.ema26 = preEma26.multiply(MACD.b25).add(trade.multiply(MACD.b2)).divide(MACD.b27,4);

        /*
         * dea = (前一日dea * 8 + 今日dif *2)/10
         */
        this.dea = preDay.getDea().multiply(MACD.b8).add(emaDif().multiply(MACD.b2)).divide(MACD.b10,4);

        this.macd = emaDif().subtract(dea).multiply(MACD.b2).setScale(3,BigDecimal.ROUND_DOWN);
        return this.macd;
    }

    public String toJSONString(){
        return JSON.toJSONString(this);
    }

}
